var ontJson = {
        "data" : [
                  {
                      "data" : "Anode3",
                      "attr" : {"rel" : "root"},
                      "metadata" : {
                    	  "nodeName" : "Anode3",
                    	  "nodeDef" : "This is the definition of Anode3",
                    	  "nodeID" : "SAM00006",
                          "Xref" : "Xref_Anode"
                      },
                      "children" : [
                          {
                              "data" : "SubnodeA",
                              "attr" : {"rel" : "isa"},
                              "metadata" : {
                            	  "nodeName" : "SubnodeA",
                            	  "nodeDef" : "Hello, SubnodeA",
                            	  "nodeID" : "SSO57867",
                                  "Xref" : "Xref_SubnodeA"
                              }
                          }, {
                              "data" : "Tomcat",
                              "attr" : {"rel" : "isa"},
                              "metadata" : {
                            	  "nodeName" : "Tomcat",
                            	  "nodeDef" : "Miao...",
                            	  "nodeID" : "HUY623727",
                                  "Xref" : "Xref_Tomcat"
                              }
                          },{
                              "data" : "Jerrymouse",
                              "attr" : {"rel" : "isa"},
                              "metadata" : {
                            	  "nodeName" : "Jerrymouse",
                            	  "nodeDef" : "Ci...",
                            	  "nodeID" : "EWQ22334",
                                  "Xref" : "Xref_Jerrymouse"
                              }
                          }
                      ]
                  },
                  {
                      "data" : "Bnode2",
                      "attr" : {"rel" : "root"},
                      "metadata" : {
                          "nodeName" : "Bnode2",
                          "nodeDef" : "The return of Bnode",
                          "nodeID" : "GFD787878",
                          "Xref" : "Xref_Bnode"
                      }
                  }      
              ]
          };
var currentNode;
var ontNodeCreated = false;
$(function(){
	$("#newOnt").click(function(){
		alert("New Ont!");
	});
	$("#addNode").click(function(){
		$("#ontTree").jstree("create");
	});
    $("#ontTree").jstree({
    	"themes" : {
    		"theme" : "classic"
    	},
    	"ui" : {
    		"selected_parent_close" : false,
    		"select_prev_on_delete" : false
    	},
    	"json_data" : ontJson,
    	"contextmenu" : {
    		"select_node" : true,
    		"show_at_node" : false
    	},
    	"types" : {
    		"valid_children" : ["root"],
    		"types" : {
    			"root" : {
    				"valid_children" : ["isa"],
    				"icon" : {
    					"image" : "http://static.jstree.com/v.1.0rc/_docs/_drive.png"
    				}
    			},
    			"isa" : {
    				"valid_children" : ["isa"],
    				"icon" : {
    					"image" : "http://bioportal.bioontology.org/images/is_a.gif"
    				}
    			}
    		}
    	},
    	"plugins" : ["themes", "ui", "json_data", "crrm", "hotkeys", "dnd", "search", "contextmenu", "types"]
    });
    $("#ontTree").bind(
    	    "select_node.jstree", function(evt, data){
    	    	//TODO define currentNode object
    	    	//jQuery's operation on JSON data.
    	    	$("#nodeName").html(data.inst.get_json()[0].metadata.nodeName);
    	    	$("#nodeDef").html(data.inst.get_json()[0].metadata.nodeDef);
    	    	$("#nodeID").html(data.inst.get_json()[0].metadata.nodeID);
    	    }
    ).bind(
    		"create_node.jstree", function(evt, data){
    			ontNodeCreated = true;
    }).bind(
    		"rename_node.jstree", function(evt, data){
    			if(ontNodeCreated){//New node created.
    				alert(data.inst.get_json()[0].metadata.nodeID);//parent node ID
        			alert(data.args[1]);//newly created node name
    				//TODO given parent node's metadata nodeID value, given newly created node name, change the json data source
        			//jQuery's operation on JSON data.
        			
    				ontNodeCreated = false;
    			}
    		}
    );
});